var dictionaryManager = {
    searchUrl: '/hzs/dictionarytype/pageFindAll',
    saveUrl: "/hzs/dictionarytype/save",
    editUrl: "/hzs/dictionarytype/edit",
    removeUrl: "/hzs/dictionarytype/remove",
    typeInfoLayer: null,
    isAdd: false,
    typeTable: null,
    init: function () {
        layui.use(['table', 'util', 'layer', 'form', 'tree'], function () {
            dictionaryManager.refreshTable();
            layui.form.on('submit(typeSearch)', dictionaryManager.search);
        });
    },

    // 刷新表格
    refreshTable: function () {
        dictionaryManager.typeTable = hs_utils.renderPageTable(dictionaryManager.tableConfig(), dictionaryManager.eventConfig());
    },

    // 打开角色信息窗口
    openFormLayer: function () {
        dictionaryManager.typeInfoLayer = layui.layer.open({
            type: 1,
            title: '字典类型',
            area: ["450px", "300px"],
            content: $("#dictionaryTypeDiv")
        });
        layui.form.on('submit(dictionaryTypeSave)', dictionaryManager.submit)
    },

    // 提交数据
    submit: function (data) {
        let url_ = dictionaryManager.editUrl;
        if (dictionaryManager.isAdd) {
            url_ = dictionaryManager.saveUrl;
        }
        hs_utils.post(url_, data.field, function (data) {
            if (data.success) {
                layui.layer.close(dictionaryManager.typeInfoLayer);
                layui.layer.msg("操作成功！");
                dictionaryManager.reloadTable();
            } else {
                layui.layer.msg(data.msg);
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    }
    ,

    // 删除
    remove: function (hsdtId) {
        hs_utils.post(dictionaryManager.removeUrl, {hsdtId: hsdtId}, function (data) {
            if (data.success) {
                layui.layer.msg("操作成功！");
                dictionaryManager.reloadTable();
            } else {
                layui.layer.msg(data.msg);
            }
        });
    },

    // 查询
    search: function (data) {
        dictionaryManager.reloadTable(data.field);
        return false;
    },

    // 重载表格
    reloadTable: function (data) {
        if (data) {
            dictionaryManager.typeTable.reload({
                where: data
            });
        } else {
            dictionaryManager.typeTable.reload();
        }
    },

    // 表格参数
    tableConfig: function () {
        return {
            elem: '#typeTable',
            url: dictionaryManager.searchUrl, //数据接口
            toolbar: '#typeTableToolBar',
            cols: [[ //表头
                {field: 'hsdtId', title: 'guid', align: "center"},
                {field: 'hsdtName', title: '类型名称', align: "center"},
                {
                    field: 'hsdtType',
                    title: '类型分类',
                    align: "center",
                    templet: function (d) {
                        if (d.hsdtType == 1) {
                            return "列表";
                        } else if (d.hsdtType == 2) {
                            return "树";
                        }
                    }
                },
                {field: 'sortNum', title: '排序号', align: "center"},
                {
                    field: 'hsdtId', title: '操作', align: "center",
                    toolbar: "#typeTableItemToolBar"
                }
            ]]
        };
    },

    // 表格事件参数
    eventConfig: function () {
        return {
            filter: 'typeTable',
            toolbar: [
                {
                    name: 'add', handler: function (obj) {
                        $('#dictionaryTypeForm')[0].reset();
                        dictionaryManager.isAdd = true;
                        dictionaryManager.openFormLayer();
                    }
                }
            ],
            tool: [
                {
                    name: 'remove', handler: function (obj) {
                        layui.layer.confirm('真的删除行么', function (index) {
                            layui.layer.close(index);
                            //向服务端发送删除指令
                            dictionaryManager.remove(obj.data.hsdtId);
                        });
                    }
                },
                {
                    name: 'edit', handler: function (obj) {
                        layui.form.val('dictionaryTypeForm', obj.data);
                        dictionaryManager.isAdd = false;
                        dictionaryManager.openFormLayer();
                    }
                }
            ],
            other: [{
                name: 'row',
                handler: function (obj) {
                    dictionaryDetailManager.setTypeData(obj.data);
                }
            }]

        };
    }
}
dictionaryManager.init();